#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 2e5 + 5;
const long long inf = 0x3f3f3f3f3f3f3f3f;
int T, n, len;
int a[maxn], b[maxn];
long long dp[maxn], q[maxn];
long long solve() {
    long long ans = 0ll;
    memset(dp, 0, sizeof(dp));
    memset(q, 0, sizeof(q));
    for (int i = 1; i <= n; i++) {
        dp[i] = max(dp[i - 1], q[a[i]]);
        q[a[i - 1]] = dp[i] + b[a[i - 1]];
        ans = max(ans, dp[i]);
    }
    return ans;
}
int main() {
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &n);
        long long ans = 0;
        for (int i = 1; i <= n; b[i] = a[i], i++) {
            scanf("%d", a + i);
            if (a[i] == a[i - 1]) ans += a[i], i--, n--;
        }
        sort(b + 1, b + n + 1);
        len = unique(b + 1, b + n + 1) - b - 1;
        for (int i = 1; i <= n; i++) a[i] = lower_bound(b + 1, b + len + 1, a[i]) - b;
        printf("%lld\n", ans + solve());
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}